package system;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public class Actor {
	
	private String myName;
	public AnimatedSprite sprite;
	public float xvel, yvel, g, x, y;
	public int power;				// how much damage the Actor does on collision.
	
	public int state;				// current state of the Actor.    default state: 0
	public int myHp;				// current hp of the Actor.       default hp: 100
	public int lifetime;			// how many frames the sprite ought to be displayed. i.e., a "timeout"
	public int curLifetime;			// a current value for the lifetime, initialized to 0 and incremented every update
	
	public Actor(String name, AnimatedSprite sprite, float x, float y){
		this(name, sprite, x, y, 0, 0, 0, -1);
	}
	public Actor(String name, AnimatedSprite sprite, float x, float y, float xvel, float yvel, int power){
		this(name, sprite, x, y, xvel, yvel, power, -1);
	}
	
	public Actor(String name, AnimatedSprite sprite, float x, float y, float xvel, float yvel, int power, int lifetime){
		this.myName = name;
		this.sprite = sprite;
		this.x = x;
		this.y = y;
		this.xvel = xvel;
		this.yvel = yvel;
		this.power = power;
		this.g = (float) 0.0;
		this.lifetime = lifetime;
		this.curLifetime = 0;
		
		this.state = 0;
		this.myHp = 100;
	}

	public void update(){
		sprite.update();

		if(myName.equals("Parallax") && x <= -480) {
			x = 480;     // horizontal looping
		}
		else {
			x = x + xvel;
		}
		y = y + yvel;
		yvel = yvel + g;
				
		if (myHp <= 0){
			state = -1; // flag as dead
		}
		
		// check if we should update a lifetime var
		if (lifetime != -1 && curLifetime < lifetime){
			curLifetime++;
			if (curLifetime >= lifetime){
				state = -1;
			}
		}
	}
	public Rectangle getBounds(){
		return new Rectangle((int)x, (int)y, sprite.getWidth(), sprite.getHeight());
	}
	
	// Getters
	public int getX(){
		return (int)this.x;
	}
	public int getY(){
		return (int)this.y;
	}
	public String getName(){
		return myName;
	}
	public BufferedImage getImage(){
		return sprite.getImage();
	}
	public int getState(){
		return state;
	}
	public int getHp(){
		return myHp;
	}
	public int getPower(){
		return power;
	}
	
	// Setter
	public void setLifetime(int newLifetime){
		lifetime = newLifetime;
	}
	public void setSprite(AnimatedSprite newSprite){
		sprite = newSprite;
	}
	public void setSprite(int index){
		if(index < 0 || index > sprite.getTotalFrames()){
			index = 0;
		}
		sprite.setFrameIndex(index);
	}
	public void setState(int newState){
		state = newState;
	}
	public void setHp(int newHp){
		if (newHp < 0){
			newHp = 0;
		}
		myHp = newHp;
	}
	public void setName(String newName){
		this.myName = newName;
	}
	public void setG(float g){
		this.g = g;
	}
	public void setPower(int i) {
		this.power = i;
		
	}



}
